using UnityEngine;
using QFramework;
using UnityEngine.Tilemaps;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class RepiatTileController : ViewController
	{

//#if UNITY_EDITOR
//        [UnityEditor.CustomEditor(typeof(RepiatTileController))]
//        public class RepeatTileMapEditor : UnityEditor.Editor
//        {
//            public override void OnInspectorGUI()
//            {
//                base.OnInspectorGUI();


//                if (GUILayout.Button("重新计算 Bounds"))  
//                {
//                    var controller = target as RepiatTileController;
//                    controller.Tilemap.CompressBounds();
//                }
//            }
//        }
//#endif




        private Tilemap mUp;
        private Tilemap mDown;
        private Tilemap mLeft;
        private Tilemap mRight;
        private Tilemap mUpLeft;
        private Tilemap mUpRight;
        private Tilemap mDownLeft;
        private Tilemap mDownRight;
        private Tilemap mCenter;


        private int AreaX = 0;
        private int AreaY = 0;

        void Start()
        {
            Tilemap.CompressBounds();
            CreartTileMaps();
            UpDataPosition();
        }

        private void Update()
        {
            if (Player.Instance && Time.frameCount % 60 == 0)
            {
                var cellPos = Tilemap.layoutGrid.WorldToCell(Player.Instance.transform.Position());
                AreaX = cellPos.x / Tilemap.size.x;
                AreaY = cellPos.y / Tilemap.size.y;
                UpDataPosition();
            }
        }

        void CreartTileMaps()
        {
            mUp = Tilemap.InstantiateWithParent(transform);
            mDown = Tilemap.InstantiateWithParent(transform);
            mLeft = Tilemap.InstantiateWithParent(transform);
            mRight = Tilemap.InstantiateWithParent(transform);
            mUpLeft = Tilemap.InstantiateWithParent(transform);
            mUpRight = Tilemap.InstantiateWithParent(transform);
            mDownLeft = Tilemap.InstantiateWithParent(transform);
            mDownRight = Tilemap.InstantiateWithParent(transform);
            mCenter = Tilemap;
        }

        void UpDataPosition()
        {
            mUp.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));

            mDown.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));

            mLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
            mRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));


            mUpLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
            mUpRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));


            mDownLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
            mDownRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
            mCenter.Position(new Vector3((AreaX + 0) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
            //mUp.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
        }
    }
}
